
//------------------------------------------------------------------------------

using Apps.Models;
using Apps.IDAL.CY;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Apps.DAL.CY
{
    public partial class CY_COAL_STRUCTURERepository : BaseRepository<CY_COAL_STRUCTURE>, ICY_COAL_STRUCTURERepository, IDisposable
    {
        public CY_COAL_STRUCTURERepository(DBContainer db) : base(db)
        {

        }

        public IQueryable<CY_COAL_STRUCTUREModel> QueryList(string YardID)
        {
            if (string.IsNullOrWhiteSpace(YardID)) YardID = "$--Empty Yard ID--$";

            IQueryable<CY_COAL_STRUCTUREModel> list = from a in Context.CY_COAL_STRUCTURE
                                                      join Stock in Context.PUB_CY_STOCKPILE on a.STOCKPILE_ID equals Stock.STOCKPILE_ID into mStock
                                                      from tStock in mStock.DefaultIfEmpty()
                                                      join Part in Context.PUB_CY_PARTITION on tStock.PARTITION_ID equals Part.PARTITION_ID into mPart
                                                      from tPart in mPart.DefaultIfEmpty()
                                                      join Yard in Context.PUB_CY_COALYARD on tPart.COALYARD_ID equals Yard.COALYARD_ID into mYard
                                                      from tYard in mYard.DefaultIfEmpty()
                                                      where tYard.COALYARD_ID == YardID || YardID == "$--Empty Yard ID--$"
                                                      join Instock in Context.CY_INSTOCK_DETAIL on a.STOCKPILE_ID equals Instock.STOCKPILE_ID into mInstock
                                                      from tInstock in mInstock.DefaultIfEmpty()
                                                      join RC in Context.PL_RC_DETAILS on tInstock.DETAIL_ID equals RC.DETAIL_ID into mRC
                                                      from tRC in mRC.DefaultIfEmpty()
                                                      join Materiel in Context.PUB_MATERIEL on tRC.MATERIEL_ID equals Materiel.MATERIEL_ID into mMateriel
                                                      from tMateriel in mMateriel.DefaultIfEmpty()
                                                      orderby tStock.BEGIN_LENGTH ascending
                                                      select new CY_COAL_STRUCTUREModel()
                                                      {
                                                          AAR = a.AAR,
                                                          COALYARD_ID = tYard.COALYARD_ID,
                                                          COALYARD_NAME = tYard.COALYARD_NAME,
                                                          CRC = a.CRC,
                                                          MT = a.MT,
                                                          PARTITION_ID = tPart.PARTITION_ID,
                                                          PARTITION_NAME = tPart.PARTITION_NAME,
                                                          PILE_TIME = a.PILE_TIME,
                                                          QNETAR_KACL = a.QNETAR_KACL,
                                                          QNETAR_MJ = a.QNETAR_MJ,
                                                          STD = a.STD,
                                                          STOCKPILE_ID = a.STOCKPILE_ID,
                                                          STOCKPILE_NAME = tStock.STOCKPILE_NAME,
                                                          TOTAL = a.TOTAL,
                                                          VAR = a.VAR,
                                                          VDAF = a.VDAF,
                                                          BEGIN_HEIGHT = tStock.BEGIN_HEIGHT,
                                                          END_HEIGHT = tStock.END_HEIGHT,
                                                          BEGIN_LENGTH = tStock.BEGIN_LENGTH,
                                                          END_LENGTH = tStock.END_LENGTH,
                                                          BEGIN_WIDTH = tStock.BEGIN_WIDTH,
                                                          END_WIDTH = tStock.END_WIDTH,
                                                          MATERIEL_ID = tMateriel.MATERIEL_ID,
                                                          MATERIEL_NAME = tMateriel.MATERIEL_NAME
                                                      };
            return list;
        }

        public IQueryable<CY_COAL_STRUCTUREModel> QueryStructureList(Expression<Func<CY_COAL_STRUCTUREModel, bool>> whereLambda)
        {
            IQueryable<CY_COAL_STRUCTUREModel> list = from a in Context.CY_COAL_STRUCTURE
                                                      join Stock in Context.PUB_CY_STOCKPILE on a.STOCKPILE_ID equals Stock.STOCKPILE_ID into mStock
                                                      from tStock in mStock.DefaultIfEmpty()
                                                      join Part in Context.PUB_CY_PARTITION on tStock.PARTITION_ID equals Part.PARTITION_ID into mPart
                                                      from tPart in mPart.DefaultIfEmpty()
                                                      join Yard in Context.PUB_CY_COALYARD on tPart.COALYARD_ID equals Yard.COALYARD_ID into mYard
                                                      from tYard in mYard.DefaultIfEmpty()
                                                      join Instock in Context.CY_INSTOCK_DETAIL on a.STOCKPILE_ID equals Instock.STOCKPILE_ID into mInstock
                                                      from tInstock in mInstock.DefaultIfEmpty()
                                                      join RC in Context.PL_RC_DETAILS on tInstock.DETAIL_ID equals RC.DETAIL_ID into mRC
                                                      from tRC in mRC.DefaultIfEmpty()
                                                      join Materiel in Context.PUB_MATERIEL on tRC.MATERIEL_ID equals Materiel.MATERIEL_ID into mMateriel
                                                      from tMateriel in mMateriel.DefaultIfEmpty()
                                                      orderby tStock.BEGIN_LENGTH ascending
                                                      select new CY_COAL_STRUCTUREModel()
                                                      {
                                                          AAR = a.AAR,
                                                          COALYARD_ID = tYard.COALYARD_ID,
                                                          COALYARD_NAME = tYard.COALYARD_NAME,
                                                          CRC = a.CRC,
                                                          MT = a.MT,
                                                          PARTITION_ID = tPart.PARTITION_ID,
                                                          PARTITION_NAME = tPart.PARTITION_NAME,
                                                          PILE_TIME = a.PILE_TIME,
                                                          QNETAR_KACL = a.QNETAR_KACL,
                                                          QNETAR_MJ = a.QNETAR_MJ,
                                                          STD = a.STD,
                                                          STOCKPILE_ID = a.STOCKPILE_ID,
                                                          STOCKPILE_NAME = tStock.STOCKPILE_NAME,
                                                          TOTAL = a.TOTAL,
                                                          VAR = a.VAR,
                                                          VDAF = a.VDAF,
                                                          BEGIN_HEIGHT = tStock.BEGIN_HEIGHT,
                                                          END_HEIGHT = tStock.END_HEIGHT,
                                                          BEGIN_LENGTH = tStock.BEGIN_LENGTH,
                                                          END_LENGTH = tStock.END_LENGTH,
                                                          BEGIN_WIDTH = tStock.BEGIN_WIDTH,
                                                          END_WIDTH = tStock.END_WIDTH,
                                                          MATERIEL_ID = tMateriel.MATERIEL_ID,
                                                          MATERIEL_NAME = tMateriel.MATERIEL_NAME
                                                      };
            return list.Where(whereLambda);
        }

        public V_CY_COAL_STRUCTURE QueryModel(string YardID)
        {
            return (from u in Context.V_CY_COAL_STRUCTURE where u.COALYARD_ID == YardID select u).FirstOrDefault();
        }
    }
}
